<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2020 All rights reserved.
// +----------------------------------------------------------------------
// | User: Simony <1362229026@qq.com>
// | wechat: Shen86579595
// +----------------------------------------------------------------------
namespace app\admin\controller;

class Statistics extends Common
{

    /**
     * User: Simony
     * Function:资产统计
     */
    public function reportForm()
    {
        //param
        $sex = input('param.sex', -1);

        $name = [];
        $date_range = input('get.date_range', '');
        $start_time = $end_time = '';
        if (!empty($date_range)) {
            $date_range = explode(' - ', $date_range);
            $start_time = strtotime($date_range[0]);
            $end_time = strtotime($date_range[1]) + 86399;
            $name['begin_date'] = $date_range[0];
            $name['end_date'] = $date_range[1];
        }

        //model
        $memberModel = model('common/member/Member');
        $packageBuyLogModel = model('common/package/PackageBuyLog');
        $rechargeLogModel = model('common/member/RechargeLog');
        $bonusModel = model('common/bonus/Bonus');
        $withdrawModel = model('common/withdraw/Withdraw');
        $contentModel = model('common/content/Content');
        $commentModel = model('common/content/Comment');
        $fileModel = model('common/member/File');

        //getData
        //注册数
        $register_where = [];
        if ($sex >= 0) $register_where[] = ['sex', 'eq', $sex];
        if (!(empty($start_time)) && !(empty($end_time))) {
            $register_where[] = ['create_time', 'egt', $start_time];
            $register_where[] = ['create_time', 'elt', $end_time];
        }
        $register['count'] = $memberModel->getCount($register_where);
        $register['title'] = '注册(人)';

        //认证会员数
        $real_verify_where[] = ['real_verify', 'eq', 1];
        if ($sex >= 0) $real_verify_where[] = ['sex', 'eq', $sex];
        if (!(empty($start_time)) && !(empty($end_time))) {
            $real_verify_where[] = ['create_time', 'egt', $start_time];
            $real_verify_where[] = ['create_time', 'elt', $end_time];
        }
        $real_verify['count'] = $memberModel->getCount($real_verify_where);
        $real_verify['title'] = '认证(人)';

        //女神
        $video_verify_where[] = ['video_verify', 'eq', 1];
        if ($sex >= 0) $video_verify_where[] = ['sex', 'eq', 2];
        if (!(empty($start_time)) && !(empty($end_time))) {
            $video_verify_where[] = ['create_time', 'egt', $start_time];
            $video_verify_where[] = ['create_time', 'elt', $end_time];
        }
        $video_verify['count'] = $sex != 1 ? $memberModel->getCount($video_verify_where) : 0;
        $video_verify['title'] = '女神认证(人)';

        //购买套餐(次)
        $package_buy_log_where = [];
        if ($sex >= 0) $package_buy_log_where[] = ['m.sex', 'eq', $sex];
        if (!(empty($start_time)) && !(empty($end_time))) {
            $package_buy_log_where[] = ['p_b_l.pay_time', 'egt', $start_time];
            $package_buy_log_where[] = ['p_b_l.pay_time', 'elt', $end_time];
        }
        $count_1 = $packageBuyLogModel->alias('p_b_l')
            ->join('member m', 'm.id= p_b_l.member_id')
            ->where($package_buy_log_where)
            ->count('p_b_l.id');
        $count_2 = $packageBuyLogModel->alias('p_b_l')
            ->join('member m', 'm.id= p_b_l.member_id')
            ->group('p_b_l.member_id')
            ->where($package_buy_log_where)
            ->count('p_b_l.id');

        $package_buy_log['count'] = $count_1 . '/' . $count_2;
        $package_buy_log['title'] = '购买套餐(次/人)';

        //充值
        $recharge_log_where = [];
        if ($sex >= 0) $recharge_log_where[] = ['m.sex', 'eq', $sex];
        if (!(empty($start_time)) && !(empty($end_time))) {
            $recharge_log_where[] = ['r_l.create_time', 'egt', $start_time];
            $recharge_log_where[] = ['r_l.create_time', 'elt', $end_time];
        }
        $sum_1 = $rechargeLogModel->alias('r_l')
            ->join('member m', 'm.id= r_l.member_id')
            ->where($recharge_log_where)
            ->sum('r_l.price');
        $sum_2 = $rechargeLogModel->alias('r_l')
            ->join('member m', 'm.id= r_l.member_id')
            ->where($recharge_log_where)
            ->sum('r_l.money');
        $recharge_log['count'] = $sum_1 . '/' . $sum_2;
        $recharge_log['title'] = '充值(现金/金币)';

        //推广奖
        $bonus_where = [];
        if ($sex >= 0) $bonus_where[] = ['m.sex', 'eq', $sex];
        if (!(empty($start_time)) && !(empty($end_time))) {
            $bonus_where[] = ['b.create_time', 'egt', $start_time];
            $bonus_where[] = ['b.create_time', 'elt', $end_time];
        }
        $sum_1 = $bonusModel->alias('b')
            ->join('member m', 'm.id= b.member_id')
            ->where($bonus_where)
            ->sum('recommend_money');
        $recommend['count'] = $sum_1;
        $recommend['title'] = '推广金币奖励';

        //佣金奖
        $sum_2 = $bonusModel->alias('b')
            ->join('member m', 'm.id= b.member_id')
            ->where($bonus_where)
            ->sum('commission_money');
        $commission['count'] = $sum_2;
        $commission['title'] = '佣金金币奖励';

        //提现（已提走的(提现完成)）
        $withdraw_where[] = ['w.state', 'eq', 3];
        if ($sex >= 0) $withdraw_where[] = ['m.sex', 'eq', $sex];
        if (!(empty($start_time)) && !(empty($end_time))) {
            $withdraw_where[] = ['w.create_time', 'egt', $start_time];
            $withdraw_where[] = ['w.create_time', 'elt', $end_time];
        }
        $sum_1 = $withdrawModel->alias('w')
            ->join('member m', 'm.id= w.member_id')
            ->where($withdraw_where)
            ->sum('w.price');
        $sum_2 = $withdrawModel->alias('w')
            ->join('member m', 'm.id= w.member_id')
            ->where($withdraw_where)
            ->sum('w.money');
        $withdraw['count'] = $sum_1 . '/' . $sum_2;
        $withdraw['title'] = '提现(现金/金币)';

        //提现手续费
        $sum_3 = $withdrawModel->alias('w')
            ->join('member m', 'm.id= w.member_id')
            ->where($withdraw_where)
            ->sum('w.tax_money');
        $tax_money['count'] = $sum_3;
        $tax_money['title'] = '提现手续费(金币)';

        //发布动态数
        $content_1_where[] = ['c.type', 'eq', 1];
        if ($sex >= 0) $content_1_where[] = ['m.sex', 'eq', $sex];
        if (!(empty($start_time)) && !(empty($end_time))) {
            $content_1_where[] = ['c.create_time', 'egt', $start_time];
            $content_1_where[] = ['c.create_time', 'elt', $end_time];
        }
        $content1['count'] = $contentModel->alias('c')
            ->join('member m', 'm.id= c.member_id')
            ->where($content_1_where)
            ->count('c.id');
        $content1['title'] = '发布动态(条)';

        //节目
        $content_2_where[] = ['c.type', 'eq', 2];
        if ($sex >= 0) $content_2_where[] = ['m.sex', 'eq', $sex];
        if (!(empty($start_time)) && !(empty($end_time))) {
            $content_2_where[] = ['c.create_time', 'egt', $start_time];
            $content_2_where[] = ['c.create_time', 'elt', $end_time];
        }
        $content2['count'] = $contentModel->alias('c')
            ->join('member m', 'm.id= c.member_id')
            ->where($content_2_where)
            ->count('c.id');
        $content2['title'] = '发布节目(条)';

        //报名
        $sign_up_where[] = ['c.type', 'eq', 1];
        if ($sex >= 0) $sign_up_where[] = ['m.sex', 'eq', $sex];
        if (!(empty($start_time)) && !(empty($end_time))) {
            $sign_up_where[] = ['c.create_time', 'egt', $start_time];
            $sign_up_where[] = ['c.create_time', 'elt', $end_time];
        }
        $sign_up['count'] = $commentModel->alias('c')
            ->join('member m', 'm.id= c.member_id')
            ->where($sign_up_where)
            ->count('c.id');
        $sign_up['title'] = '报名(条)';

        //动态评论
        $comment_1_where[] = ['c.type', 'eq', 1];
        if ($sex >= 0) $comment_1_where[] = ['m.sex', 'eq', $sex];
        if (!(empty($start_time)) && !(empty($end_time))) {
            $comment_1_where[] = ['c_c.create_time', 'egt', $start_time];
            $comment_1_where[] = ['c_c.create_time', 'elt', $end_time];
        }
        $comment1['count'] = $commentModel->alias('c_c')
            ->join('member m', 'm.id= c_c.member_id')
            ->join('content c', 'c.id= c_c.content_id')
            ->where($comment_1_where)
            ->count('c.id');
        $comment1['title'] = '动态评论(条)';

        //节目评论
        $comment_2_where[] = ['c.type', 'eq', 2];
        if ($sex >= 0) $comment_2_where[] = ['m.sex', 'eq', $sex];
        if (!(empty($start_time)) && !(empty($end_time))) {
            $comment_2_where[] = ['c_c.create_time', 'egt', $start_time];
            $comment_2_where[] = ['c_c.create_time', 'elt', $end_time];
        }
        $comment2['count'] = $commentModel->alias('c_c')
            ->join('member m', 'm.id= c_c.member_id')
            ->join('content c', 'c.id= c_c.content_id')
            ->where($comment_2_where)
            ->count('c.id');
        $comment2['title'] = '节目评论(条)';

        //新增公开照片
        $file_1_where[] = ['f.type', 'eq', 1];
        if ($sex >= 0) $file_1_where[] = ['m.sex', 'eq', $sex];
        if (!(empty($start_time)) && !(empty($end_time))) {
            $file_1_where[] = ['f.create_time', 'egt', $start_time];
            $file_1_where[] = ['f.create_time', 'elt', $end_time];
        }
        $file1['count'] = $fileModel->alias('f')
            ->join('member m', 'm.id= f.member_id')
            ->where($file_1_where)
            ->count('f.id');
        $file1['title'] = '新增公开照片(张)';


        //新增红包照片
        $file_3_where[] = ['f.type', 'eq', 3];
        if ($sex >= 0) $file_2_where[] = ['m.sex', 'eq', $sex];
        if (!(empty($start_time)) && !(empty($end_time))) {
            $file_3_where[] = ['f.create_time', 'egt', $start_time];
            $file_3_where[] = ['f.create_time', 'elt', $end_time];
        }
        $file3['count'] = $fileModel->alias('f')
            ->join('member m', 'm.id= f.member_id')
            ->where($file_3_where)
            ->count('f.id');
        $file3['title'] = '新增红包照片(张)';


        //新增阅后即焚照片
        $file_2_where[] = ['f.type', 'eq', 2];
        if ($sex >= 0) $file_2_where[] = ['m.sex', 'eq', $sex];
        if (!(empty($start_time)) && !(empty($end_time))) {
            $file_2_where[] = ['f.create_time', 'egt', $start_time];
            $file_2_where[] = ['f.create_time', 'elt', $end_time];
        }
        $file2['count'] = $fileModel->alias('f')
            ->join('member m', 'm.id= f.member_id')
            ->where($file_2_where)
            ->count('f.id');
        $file2['title'] = '新增阅后即焚照片(张)';

        $data_list = [
            $register,
            $real_verify,
            $video_verify,
            $package_buy_log,
            $recharge_log,
            $recommend,
            $commission,
            $withdraw,
            $tax_money,
            $content1,  ////1：动态、2：节目  内容
            $content2,
            $sign_up,
            $comment1,  //1：动态、2：节目  评论
            $comment2,
            $file1, //1：照片、2：阅后即焚、3：红包照片
            $file3,
            $file2
        ];

        $this->assign('data_list', $data_list);
        $this->assign('name', $name);
        $this->assign('sexs', $memberModel->sexs);
        return $this->fetch();
    }


    public function account()
    {
        $memberModel = model('common/member/Member');

        $member_data = [];
        foreach ($memberModel->sexs as $sex => $sex_name) {
            $member_data[] = [
                'sex_name' => $sex_name,
                'total_balance' => $memberModel->getSum(['sex' => $sex], 'available_balance') + $memberModel->getSum(['sex' => $sex], 'unissued_balance'),
                'available_balance' => $memberModel->getSum(['sex' => $sex], 'available_balance'),
                'count' => $memberModel->getCount(['sex' => $sex]),
                'not_real_verify_count' => $memberModel->getCount(['sex' => $sex, 'real_verify' => 0]),
                'real_verify_count' => $memberModel->getCount(['sex' => $sex, 'real_verify' => 1]),
                'video_verify_count' => $sex == 2 ? $memberModel->getCount(['sex' => $sex, 'video_verify' => 1]) : '--',
                'vip_count' => $memberModel->getCount(['sex' => $sex, 'level' => 2]),
            ];
        }

        array_unshift($member_data, [
            'sex_name' => '所有',
            'count' => $memberModel->getCount(),
            'total_balance' => $memberModel->getSum([], 'available_balance') + $memberModel->getSum([], 'unissued_balance'),
            'available_balance' => $memberModel->getSum([], 'available_balance'),
            'not_real_verify_count' => $memberModel->getCount(['real_verify' => 0]),
            'real_verify_count' => $memberModel->getCount(['real_verify' => 1]),
            'video_verify_count' => $memberModel->getCount(['sex' => 2, 'video_verify' => 1]),
            'vip_count' => $memberModel->getCount(['level' => 2]),
        ]);


        $this->assign('member_data', $member_data);
        return $this->fetch();
    }
}